class Solution:
    def maxResult(self, nums: list, k: int) -> int:
        deque = []
        cur = 0
        for i in range(len(nums) - 1, -1, -1):
            cur = nums[i] + (nums[deque[0]] if deque else 0)
            while deque and cur > nums[deque[-1]]:
                deque.pop()
            deque.append(i)
            if deque[0] - k == i:
                deque.pop(0)
            nums[i] = cur
        return nums[0]
